package com.hyh.data;

import com.hyh.media.AudioTrackUtil;
import com.platform.GosSession;
import com.platform.net.NetPlatform;
import com.platform.response.BaseResponse;
import com.platform.response.FrameTransferResponse;

import java.net.DatagramPacket;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/1/26.
 */

public class PlatModule implements GosSession.OnResponseCallback {

    private static PlatModule platModule;
    private List<GosSession.OnResponseCallback> responseCallbacks =  new ArrayList<>();

    private PlatModule(){
        GosSession.getInstance().addRecvCallback(this);
    }

    public static synchronized PlatModule getInstance(){
        if(platModule==null){
            platModule = new PlatModule();
        }
        return platModule;
    }

    public void addRecvCallback(GosSession.OnResponseCallback onRecvCallback){
        if(!responseCallbacks.contains(onRecvCallback)){
            responseCallbacks.add(onRecvCallback);
        }
    }

    public void removeRecvCallback(GosSession.OnResponseCallback onRecvCallback){
        if(responseCallbacks.contains(onRecvCallback)){
            responseCallbacks.remove(onRecvCallback);
        }
    }

    private void handleResponse(BaseResponse response){
        String msgType = response.MessageType;
        switch (msgType){
            case BaseResponse.MsgType.HeartBeatResponse:
                break;
            case BaseResponse.MsgType.FrameTransferResponse:
                FrameTransferResponse transferResponse = (FrameTransferResponse) response;
                byte buf[] = transferResponse.Body.buf;
                AudioTrackUtil.getInstance().startPlay();
                AudioTrackUtil.getInstance().write(buf);
                break;
        }
    }
    @Override
    public void responseCallback(BaseResponse response) {
        handleResponse(response);
        for(GosSession.OnResponseCallback onResponseCallback:responseCallbacks){
            onResponseCallback.responseCallback(response);
        }
    }

    public void startHeartBeat(){
        GosSession.getInstance().startHeartBeat();
    }

    public void stopHeartBeat(){
        GosSession.getInstance().stopHeartBeat();
    }


    public void transferFrame(byte frameData[]){
        GosSession.getInstance().transferFrame(frameData);
    }
}

